<?php
    session_start();
    require_once('userlevel.php'); //Require/include the permissions file
    require_once('template.php'); //Require/include the template file
    require_once('db.php'); //Require/include the file with the database connection information
    require_once('functions.php'); //Require/include the functions file
     
    if (!$_SESSION['user_loggedin']) {
        die ("Not logged in... <script>document.location.href='login.php'</script>");
    } elseif (!($_SESSION['userlevel'] >= $userlevel['upddateallfiles'])) {
        die (errorpage ("Your user level is not high enough to use this feature!", 'Update All Files'));
    } else {
        //Connect to DB and connect to the database using db.php
        $db = new my_db;
         
         
        template_headtag('Update All Files');
        template_header();
        template_left();
        echo "<div class='pageheadertext'>Update All Files</div><br/>";
         
        echo "<div align = 'center'>";
        echo "<table class='table' width='100%'>";
         
        $db->query("SELECT * FROM trackedfiles ORDER BY filepath,filename");
         
        // keep getting the next row until there are no more to get
        while ($db->next_record()) {
            $id = $db->f('id');
            $filepath = $db->f('filepath');
            if ($lastfilepath !== $filepath) {
                echo "<tr class='tabledirtitle'>";
                echo "<td colspan='3'>";
                echo "$filepath";
                echo "</td></tr>";
                $rowstyle = 'oddrow';
            }
            $lastfilepath = $filepath;
            $filename = $db->f('filename');
            $latestrev = $db->f('latestrev');
            if ($rowstyle == 'oddrow') {
                $rowstyle = 'evenrow';
            } else {
                $rowstyle = 'oddrow';
            }
            $status = updatefilestatus ($latestrev, "$filepath$filename");
            if ($status == 'badfile') {
                echo "<tr class='$rowstyle'>";
                echo "<td class='tablealert'>";
                echo "$filename";
                echo "</td><td class='tablealert'>";
                echo "File does not exist!";
                if ($_SESSION['userlevel'] >= $userlevel['stoptrackingfile']) {
                    echo "</td><td class='tablealert'>";
                    echo "<a href='stoptrackingfile.php?id=$id' title='Stop tracking file'><img src='images/tables/cross.png' width='16' height='16' border='0' alt='Stop tracking file' /></a></td>";
                } else {
                    echo "</td><td>";
                }
                echo "</tr>";
            } elseif($status == 'nochange') {
                echo "<tr class='$rowstyle'>";
                echo "<td>";
                echo "$filename";
                echo "</td><td>";
                echo "";
                echo "</td><td><img src='images/tables/accept.png' width='16' height='16' border='0' alt='No Change' />";
                echo "</td></tr>\n";
            } elseif($status == 'isdir') {
                echo "<tr class='$rowstyle'>";
                echo "<td class='tablealert'>";
                echo "$filename";
                echo "</td><td class='tablealert'>";
                echo "Can not be tracked, is a director";
                echo "</td><td class='tablealert'>";
                echo "</td></tr>\n";
            } elseif($status == 'cannotread') {
                echo "<tr class='$rowstyle'>";
                echo "<td class='tablealert'>";
                echo "$filename";
                echo "</td><td class='tablealert'>";
                echo "Can not read file, permission denied!";
                echo "</td><td class='tablealert'><img src='images/messagebox_warning.png' width='16' height='16' border='0' alt='Cannot Read File' />";
                echo "</td></tr>\n";
            } else {
                echo "<tr class='$rowstyle'>";
                echo "<td class='tableupdate'>";
                echo "$filename";
                echo "</td><td class='tableupdate'>";
                echo updatefile ($id, $status, $revsummary, $filepath, $filename);
                echo "</td><td class='tableupdate'><img src='images/tables/flag.png' width='16' height='16' border='0' alt='File Updated' />";
                echo "</td></tr>\n";
            }
             
        }
        echo "</table></div><br/>";
        template_footer();
    }
?>
